package com.cheetah.controller.protobuf;

import com.cheetah.annotation.ProtobufController;
import com.cheetah.annotation.RequestMapping;
import com.cheetah.annotation.RunExecutor;
import com.cheetah.constants.MessageTypeEnums;
import com.cheetah.constants.ProtobufConstants;
import com.cheetah.dto.MessageDTO;
import com.cheetah.protocol.Request;
import com.cheetah.protocol.Response;
import com.cheetah.protostuff.ProtostuffSerialization;
import com.cheetah.service.MessageService;
import com.cheetah.vo.MessageVO;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author Administrator
 * @Date 2020/9/4
 * @Version V1.0
 * @Description:
 **/
@Component
@ProtobufController
@Slf4j
@RunExecutor("message-executor")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(mapping = ProtobufConstants.SEND_MESSAGE,auth = false)
    public Response sendMessage(Request request, Channel channel){
        log.info(Arrays.toString(request.getBody()));
        MessageVO messageVO = ProtostuffSerialization.deserialize(request.getBody(), MessageVO.class);
        MessageDTO messageDTO = messageService.sendMessage(messageVO);
        return Response.builder().data(messageDTO).build();
    }

//    public static void main(String[] args) {
//        MessageVO messageVO = new MessageVO();
//        messageVO.setSender("111");
//        messageVO.setType("TEXT");
//        messageVO.setId(222L);
//        System.out.println(Arrays.toString(ProtostuffSerialization.serialize(messageVO)));
//    }
}
